package com.atguigu.tingshu.order.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;

	@Operation(summary = "内部接口：查询订单统计数据")
	// albumId-> 数量
	@GetMapping("getOrderSaleStates/{date}")
	public Result<Map<Long , Integer>> getOrderSaleStates(@PathVariable("date")String date){
		Map<Long, Integer> map = orderInfoService.getOrderSaleStates(date);
		return Result.ok(map);
	}

	//
	@Operation(summary = "内部接口：根据订单号查询订单详情")
	@GuiguLogin
	@GetMapping("findOrderInfoByOrderNo/{orderNo}")
	public Result<OrderInfo> findOrderInfoByOrderNo(@PathVariable("orderNo")String orderNo){
		return Result.ok(orderInfoService.getOne(Wrappers.lambdaQuery(OrderInfo.class)
				.eq(OrderInfo::getOrderNo , orderNo)));
	}
//	/api/order/orderInfo/findUserPage/1/10
	@Operation(summary = "查询用户的订单分页列表")
	@GetMapping("/findUserPage/{pageNum}/{pageSize}")
	@GuiguLogin
	public Result<Page<OrderInfo>> findUserPage(@PathVariable("pageNum")Long pageNum ,
												@PathVariable("pageSize")Long pageSize){
		Page<OrderInfo> orderInfoPage = orderInfoService.findUserPage(pageNum,pageSize);
		return Result.ok(orderInfoPage);
	}



	///api/order/orderInfo/getOrderInfo/1913420818726113280
	@Operation(summary = "根据订单号查询订单详情")
	@GuiguLogin
	@GetMapping("getOrderInfo/{orderNo}")
	public Result<OrderInfo> getOrderInfo(@PathVariable("orderNo")String orderNo){
		return Result.ok(orderInfoService.getOrderInfoByOrderNo(orderNo));
	}
//	submitOrder
	@Operation(summary = "提交订单：保存订单 和交易记录")
	@GuiguLogin //必须登录
	@PostMapping("submitOrder")
	public Result submitOrder(@RequestBody OrderInfoVo orderInfoVo){
		String orderNo = orderInfoService.submitOrder(orderInfoVo);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("orderNo" , orderNo);
		return Result.ok(jsonObject);
	}
	///api/order/orderInfo/trade
	@Operation(summary = "订单购买：跳转到订单确认页回显要购买的订单数据")
	@PostMapping("trade")
	@GuiguLogin
	public Result trade(@RequestBody TradeVo tradeVo){
		OrderInfoVo vo =
		orderInfoService.trade(tradeVo);
		return Result.ok(vo);
	}
}

